<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
      v-loading="loading"
      style="margin-top: 20px"
      :data="list"
      :props="defaultProps"
      node-key="catId"
      ref="menuTree"
      @node-click="NodeClick"
      :filter-node-method="filterNode"
      :highlight-current = "true">
    </el-tree>
  </div>
</template>

<script>

import {list} from "../../api/goods/category";

export default {
  name: 'CategoryMenu',
  data() {
    return {
      filterText: "",
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      loading: false,
    }
  },
  created() {
    this.getList()
  },
  watch: {
    filterText(val) {
      this.$refs.menuTree.filter(val);
    }
  },
  methods: {
    //树节点过滤
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    getList() {
      this.loading = true
      list()
        .then(request => {
          this.list = request.data
          this.loading = false
        })
    },
    NodeClick(data, node, components) {
      this.$emit('tree-node-click', data, node, components)
    }
  }
}
</script>

<style scoped>
</style>
